package com.tencent.cloud.uikit.utils;

import android.app.AppOpsManager;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Binder;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.view.WindowManager;

import java.lang.reflect.Method;

/**
 * @author shockcao
 * @date 2018/6/22
 */

public class FlowWindowPermissionUtils {

    public static boolean hasFloatingPermission(Context context) {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            return Settings.canDrawOverlays(context);
//        } else {
        return hasPermissionBelowMarshmallow(context);
//        }
    }

    public static boolean hasFloatingPermissionOnActivityResult(Context context) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
            return hasPermissionForO(context);
        }
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            return Settings.canDrawOverlays(context);
//        } else {
        return hasPermissionBelowMarshmallow(context);
//        }
    }

    /**
     * 6.0以下判断是否有权限
     * 理论上6.0以上才需处理权限，但有的国内rom在6.0以下就添加了权限
     * 其实此方式也可以用于判断6.0以上版本，只不过有更简单的canDrawOverlays代替
     */
    private static boolean hasPermissionBelowMarshmallow(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                AppOpsManager manager = (AppOpsManager) context.getSystemService(Context
                        .APP_OPS_SERVICE);
                Method dispatchMethod = AppOpsManager.class.getMethod("checkOp", int.class, int
                        .class, String.class);
                //AppOpsManager.OP_SYSTEM_ALERT_WINDOW = 24
                return AppOpsManager.MODE_ALLOWED == (Integer) dispatchMethod.invoke(
                        manager, 24, Binder.getCallingUid(), context.getApplicationContext()
                                .getPackageName());
            } catch (Exception e) {
                return false;
            }
        } else {
            return false;
        }
    }


    /**
     * 用于判断8.0时是否有权限，仅用于OnActivityResult
     * 针对8.0官方bug:在用户授予权限后Settings.canDrawOverlays或checkOp方法判断仍然返回false
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    private static boolean hasPermissionForO(Context context) {
        try {
            WindowManager mgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            if (mgr == null) {
                return false;
            }
            View viewToAdd = new View(context);
            WindowManager.LayoutParams params = new WindowManager.LayoutParams(0, 0,
                    android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O ?
                            WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY : WindowManager
                            .LayoutParams.TYPE_SYSTEM_ALERT,
                    WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams
                            .FLAG_NOT_FOCUSABLE,
                    PixelFormat.TRANSPARENT);
            viewToAdd.setLayoutParams(params);
            mgr.addView(viewToAdd, params);
            mgr.removeView(viewToAdd);
            return true;

//            AppOpsManager appOpsMgr = (AppOpsManager) context.getSystemService(Context
// .APP_OPS_SERVICE);
//            if (appOpsMgr == null) {
//                return false;
//            }
//            int mode = appOpsMgr.checkOpNoThrow("android:system_alert_window", android.os
// .Process.myUid(), context
//                    .getPackageName());
//
//
//            return mode == AppOpsManager.MODE_ALLOWED || mode == AppOpsManager.MODE_IGNORED;
        } catch (Exception e) {
        }
        return false;
    }


}
